package com.ys.controller.web.account;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.account.TcwAdvanceAccountDetail;
import com.ys.entity.account.TcwAdvanceAccountReimbursement;
import com.ys.entity.sys.YsGridButton;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.enume.sys.YsGridButtonEnum;
import com.ys.service.account.TcwAdvanceAccountReimbursementService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预支账户报销单
 */
@Controller
@RequestMapping("/web/account/tcwAdvanceAccountReimbursement")
public class WebTcwAdvanceAccountReimbursementController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwAdvanceAccountReimbursementController.class);

	@Autowired
	private CommonApiService<TcwAdvanceAccountReimbursement> commonApiService;

    @Autowired
    private TcwAdvanceAccountReimbursementService tcwAdvanceAccountReimbursementService;

    @Autowired
    private YspTaskService yspTaskService;

    private final long GUID = 17L;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            TcwAdvanceAccountReimbursement entity = new TcwAdvanceAccountReimbursement();
            String bid = commonApiService.getCommonNextBid(MenuId.TCW_AdvanceAccountReimbursement);
            entity.setBid(bid);
            Map<String, Object> extraParam = new HashMap<>();
            extraParam.put("accountType", 0);
            extraParam.put("status", 1);
            extraParam.put("payStatus", 0);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_AdvanceAccountReimbursement, new ArrayList<Map<String, Object>>());

            return commonApiService.toAdd(model, entity, MenuId.TCW_AdvanceAccountReimbursement, extraParam, subGridConfig);
            //return "account/tcwAdvanceAccountReimbursement/toAdd";
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }



    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String,Object> entity = tcwAdvanceAccountReimbursementService.getExtraParam(bid);
            YsProcInst ysProcInst = new YsProcInst().setBid(entity.get("bid").toString()).queryFirst();

            Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
            model.addAttribute("onProcess", onProcess);

            List<Map<String, Object>> itemList = tcwAdvanceAccountReimbursementService.getItemList(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_AdvanceAccountReimbursement, itemList);

            model.addAttribute("_ys_vyhzhmc", entity.get("_ys_vyhzhmc"));

            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            List gridBtnList = new ArrayList<>();

            YsGridButton ysGridButton = new YsGridButton().setGridId(GUID).setEvent("event-to-export").queryFirst();
            String name = ysGridButton.getName();
            String event = ysGridButton.getEvent();
            String icon = ysGridButton.getIcon();
            String style = YsGridButtonEnum.StyleType.getNameStyle(ysGridButton.getStyleType());
            gridBtnList.add(new SubGridConfigDto.ButtonDto(name, event, icon, style));

            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(gridBtnList);
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(gridBtnList);
            }
            return commonApiService.toEdit(model,Long.parseLong(entity.get("id").toString()), new TcwAdvanceAccountReimbursement(), MenuId.TCW_AdvanceAccountReimbursement, entity, subGridConfig);
            //return "account/tcwAdvanceAccountReimbursement/toEdit";
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


}






